<?php
declare (strict_types = 1);

namespace app\index\controller;

use app\Request;
use think\facade\Session;
use think\facade\Cookie;
use app\common\service\AdminUser as S;

class Wxdl extends \app\BaseController
{
    /**
     * 首页
     */
    public function index(){
        //dump(1);die;
        //$ckt=$this->_getAccessToken(false);
        //echo($ckt);
        if(S::isLogin())
        {
            $info=Session::get('xfuser');
            $token=Cookie::get('xftoken');
           // dump($token);
        }
        else{
            header("Location:" . '/wx/wxlogin');
        }
    }
    public function wxlogin(Request $request)
    {
        $rurl=$request->param("rurl");
        if($rurl) Cookie::set("rurl",$rurl);
        $str="http://xf.xingtaiwang.net/wx/getToken";
        $redirect_uri=urlencode($str);
       // dump($redirect_uri);die;
        //通过code获得 access_token + openid
        $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx67c0c83451e9a252&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
        header("Location:" . $url);
    }

    public function getToken()
	{
		$code = $_GET["code"];
		//通过code获得 access_token + openid
        $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx67c0c83451e9a252&secret=501b8933fd15f9f26b437cef82df6486&code=" .$code . "&grant_type=authorization_code";
        $jsonResult =$this->https_request($url);
        $resultArray = json_decode($jsonResult, true);
	    //dump($resultArray);
         $access_token = $resultArray["access_token"];;
        // echo($access_token);
         $openid = $resultArray["openid"];
		
        $getmsgurl="https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
        $msgResult =$this->https_request($getmsgurl);
        $msgjson = json_decode($msgResult, true);

		// 获取用户基本信息
        // 把用户昵称、头像 openid 获取到后存入数据库，并写入登录cookies
        $nickname= urlencode($msgjson["nickname"]);
        $headimg=$msgjson["headimgurl"];
        $ip=request()->ip();
        
        $post_data =["openid"=>$openid,"nickname"=>$nickname,"headimgurl"=>$headimg,"ip"=>$ip]; 

        $result= S::wxlogin($post_data);
        if($result['code']==202 || $result['code']==200)
        {
            $rurl=Cookie::get('rurl');
            echo($rurl);
            if($rurl){header("Location:".$rurl);}
            else{header("Location:/index/index");}
        }
        else{
            echo($result['code']);
        }
        //登录成功写入ck111213
	}
    //php 获取curl获取接口数据
    function https_request($url, $data = null)
	{
	    $curl = curl_init();
	    curl_setopt($curl, CURLOPT_URL, $url);
	    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
	    if (!empty($data)){
	        curl_setopt($curl, CURLOPT_POST, 1);
	        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
	    }
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	    $output = curl_exec($curl);
	    curl_close($curl);
	    return $output;
	}
	
}
